package ve.basura.entrada.form;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.KeyListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.layout.RowData;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.widgets.Text;
import org.springframework.beans.factory.annotation.Autowired;

import com.aseo.model.entity.hibernate.Chofer;
import com.aseo.model.entity.hibernate.Vehiculo;

import ve.basura.entrada.bdl.EntradaSalidaBDL;
import ve.basura.entrada.core.SWTCore;
import ve.basura.entrada.listener.AddListener;
import ve.basura.entrada.main.EntradaMain;
import ve.basura.entrada.util.ApplicationContextUtil;
import ve.basura.entrada.util.EntradaSalidaUtil;

public class Window  {

	private	Shell shell;
	private EntradaSalidaUtil entradaSalidaUtil;
	
	private SeleccionChoferDialog seleccionChoferDialog;
	private SeleccionVehiculoDialog seleccionVehiculoDialog;
	
	private Vehiculo vehiculo;
	private Chofer chofer;
	
	//Componentes Busqueda Vehiculo
	private Text valueBusquedaText;
	private Label codigoVehiculoValueLabel;
	private Label placaVehiculoValueLabel; 
	private Label descripcionValueLabel;
	private Label capacidadValueLabel;
	private Label organismoValueLabel; 
	private Label imageDeleteVehiculo;
	private Combo seleccionTipoBusquedaCombo ;
	
	//Componenetes Busqueda Chofer
	private Text valueBusquedaChoferText;
	private Combo seleccionTipoBusquedaChoferCombo;
	private Button buscarChoferButton;
	private Button abrirCatalogoChoferButton;
	private Label codigoChoferValueLabel;
	private Label cedulaChoferValueLabel;
	private Label nombreChoferValueLabel;
	private Label imageDeleteChofer;
	
	//Componentes peso 
	Text pesoTaraValueText ;
	
	//Componentes de la ventana en general 
	Label tituloLabel;
	Group group;
	Group groupSeleccionPeso;
	
	//dialog
	MessageBox dialog;
	
	//parametros de configuracion
	private boolean entrada; 
	private boolean peso; 
	
	@Autowired
	EntradaSalidaBDL entradaSalidaBDL;
	
	public Window(){
		
	}
	
	public Window(Shell parent, EntradaSalidaUtil entradaSalidaUtil ) {
		this.shell =(Shell) parent;	
		this.entradaSalidaUtil = entradaSalidaUtil;
		generateGUI();
		loadModifyWindowSettings();
	}

	public void generateGUI(){
		
		//configuramos la ventana
		shell.setText("Aplicacion Entrada/Salida");
		Image appIcon = entradaSalidaUtil.getImagesMap().get("applicationIcon");
		shell.setImage(appIcon);
		
		//inicializacion del dialog
		dialog = new MessageBox (shell, SWT.OK|SWT.ICON_WARNING);
		
		//griddata usado en cada componente
		GridData gridData;
		tituloLabel = new Label(this.shell,SWT.CENTER);
		
		gridData = new GridData();
		gridData.grabExcessHorizontalSpace= true;
		gridData.horizontalAlignment=SWT.FILL;
		tituloLabel.setLayoutData(gridData);
		
		//Grupo externo
		group = new Group(shell, SWT.SHADOW_ETCHED_IN);
		gridData = new GridData();
		gridData.grabExcessHorizontalSpace= true;
		gridData.horizontalAlignment=SWT.FILL;
		group.setLayoutData(gridData);
		
		//Grid externo
		GridLayout gridLayoutGroup = new GridLayout(1,false);
		gridLayoutGroup.marginHeight=10;
		gridLayoutGroup.marginRight=10;
		gridLayoutGroup.marginLeft = 10;
		gridLayoutGroup.verticalSpacing=10;
		group.setLayout(gridLayoutGroup);
		
		Button aceptarButton = new Button(shell,SWT.PUSH);
		
		//-------- Grupo Seleccion Vehiculo -------------------------------------------------
		Group groupSeleccionVehiculo = new Group(group, SWT.SHADOW_ETCHED_IN);
		groupSeleccionVehiculo.setText(entradaSalidaUtil.getProperty("window.seleccionVehiculoLabel"));
		groupSeleccionVehiculo.setLayoutData(gridData);
		GridLayout gridSeleccionVehiculoLayoutGroup = new GridLayout(3,true);
		gridSeleccionVehiculoLayoutGroup.marginHeight=10;
		gridSeleccionVehiculoLayoutGroup.marginRight=10;
		gridSeleccionVehiculoLayoutGroup.marginLeft=10;
		gridSeleccionVehiculoLayoutGroup.verticalSpacing=10;
		groupSeleccionVehiculo.setLayout(gridSeleccionVehiculoLayoutGroup);
		
		//fila1
		Composite compositeBusquedaVehiculo = new Composite(groupSeleccionVehiculo,SWT.SHADOW_NONE);
		gridData = new GridData(SWT.FILL,SWT.NULL,true,false);
		gridData.horizontalSpan=3;
		compositeBusquedaVehiculo.setLayoutData(gridData);
		
		FormLayout formLayout = new FormLayout();
		formLayout.marginLeft=0;
		formLayout.marginBottom=0;
		formLayout.marginTop=0;
		formLayout.marginRight=0;
		compositeBusquedaVehiculo.setLayout(formLayout);
		
		//fila2
		Button abrirCatalogoVehiculoButton = new Button(groupSeleccionVehiculo,SWT.PUSH|SWT.RIGHT);
		abrirCatalogoVehiculoButton.setLayoutData(new GridData(GridData.END,GridData.VERTICAL_ALIGN_CENTER,true,true,3,0));
		
		//fila3
		Label codigoVehiculoLabel = new Label(groupSeleccionVehiculo,SWT.RIGHT);
		gridData = new GridData(GridData.FILL, GridData.END,true,false);
		codigoVehiculoLabel.setLayoutData(gridData);
		codigoVehiculoLabel.setText(entradaSalidaUtil.getProperty("window.codigoVehiculoLabel"));
		
		codigoVehiculoValueLabel = new Label(groupSeleccionVehiculo, SWT.LEFT  );
		codigoVehiculoValueLabel.setText("-- --");
		gridData = new GridData(GridData.FILL, GridData.HORIZONTAL_ALIGN_BEGINNING,true,false);
		gridData.grabExcessHorizontalSpace= true;
		gridData.horizontalSpan=2;
		codigoVehiculoValueLabel.setLayoutData(gridData);
		
		//fila4
		Label placaVehiculoLabel = new Label(groupSeleccionVehiculo,SWT.RIGHT|SWT.FILL);
		gridData = new GridData(GridData.FILL, GridData.END,true,false);
		placaVehiculoLabel.setLayoutData(gridData);
		placaVehiculoLabel.setText(entradaSalidaUtil.getProperty("window.placaLabel"));
		placaVehiculoValueLabel = new Label(groupSeleccionVehiculo, SWT.LEFT|SWT.FILL);
		placaVehiculoValueLabel.setText("-- --");
		gridData = new GridData(GridData.FILL, GridData.CENTER,true,false);
		gridData.grabExcessHorizontalSpace= true;
		gridData.horizontalSpan=2;
		placaVehiculoValueLabel.setLayoutData(gridData);
		
		//fila5
		Label descripcionLabel = new Label(groupSeleccionVehiculo,SWT.RIGHT);
		gridData = new GridData(GridData.FILL, GridData.END,true,false);
		descripcionLabel.setLayoutData(gridData);
		descripcionLabel.setText(entradaSalidaUtil.getProperty("window.descripcionLabel"));
		
		descripcionValueLabel = new Label(groupSeleccionVehiculo, SWT.LEFT|SWT.FILL);
		descripcionValueLabel.setText("-- --");
		gridData = new GridData(GridData.FILL, GridData.CENTER,true,false);
		gridData.grabExcessHorizontalSpace= true;
		gridData.horizontalSpan=2;
		descripcionValueLabel.setLayoutData(gridData);
		
		//fila6
		Label clasificacionLabel = new Label(groupSeleccionVehiculo,SWT.RIGHT);
		gridData = new GridData(GridData.FILL, GridData.END,true,false);
		clasificacionLabel.setLayoutData(gridData);
		clasificacionLabel.setText(entradaSalidaUtil.getProperty("window.clasificacionLabel"));
		final Label clasificacionValueLabel = new Label(groupSeleccionVehiculo, SWT.LEFT|SWT.FILL);
		clasificacionValueLabel.setText("-- --");
		gridData = new GridData(GridData.FILL, GridData.CENTER,true,false);
		gridData.grabExcessHorizontalSpace= true;
		gridData.horizontalSpan=2;
		clasificacionValueLabel.setLayoutData(gridData);
		
		//fila7
		Label capacidadLabel = new Label(groupSeleccionVehiculo,SWT.RIGHT);
		gridData = new GridData(GridData.FILL, GridData.END,true,false);
		capacidadLabel.setLayoutData(gridData);
		capacidadLabel.setText(entradaSalidaUtil.getProperty("window.capacidadLabel"));
		capacidadValueLabel = new Label(groupSeleccionVehiculo, SWT.LEFT|SWT.FILL);
		capacidadValueLabel.setText("-- --");
		gridData = new GridData(GridData.FILL, GridData.CENTER,true,false);
		gridData.grabExcessHorizontalSpace= true;
		gridData.horizontalSpan=2;
		capacidadValueLabel.setLayoutData(gridData);
		
		//fila8
		Label organismoLabel = new Label(groupSeleccionVehiculo,SWT.RIGHT);
		gridData = new GridData(GridData.FILL, GridData.END,true,false);
		organismoLabel.setLayoutData(gridData);
		organismoLabel.setText(entradaSalidaUtil.getProperty("window.organismoLabel"));
		organismoValueLabel = new Label(groupSeleccionVehiculo, SWT.LEFT|SWT.FILL);
		organismoValueLabel.setText("-- --");
		gridData = new GridData(GridData.FILL, GridData.CENTER,true,false);
		gridData.grabExcessHorizontalSpace= true;
		gridData.horizontalSpan=2;
		organismoValueLabel.setLayoutData(gridData);

		//fila9
		Composite compositeStatusSeleccionVehiculo= new Composite(groupSeleccionVehiculo,SWT.BOTTOM);
		gridData = new GridData(SWT.FILL,GridData.VERTICAL_ALIGN_END,true,false);
		gridData.horizontalSpan=3;
		
		RowLayout rowLayout =  new RowLayout();
		rowLayout.marginHeight=10;
		compositeStatusSeleccionVehiculo.setLayoutData(gridData);
		compositeStatusSeleccionVehiculo.setLayout(rowLayout);
		
		Font fuente = new Font((Display) shell.getDisplay(),"Arial",8,SWT.BOLD);
		Label statusSeleccionVehiculo = new Label(compositeStatusSeleccionVehiculo,SWT.LEFT);
		statusSeleccionVehiculo.setText("Vehiculo Seleccionado: ");
		statusSeleccionVehiculo.setFont(fuente);
		
		imageDeleteVehiculo = new Label(compositeStatusSeleccionVehiculo,SWT.LEFT);
		imageDeleteVehiculo.setImage(entradaSalidaUtil.getImagesMap().get("deleteImage"));
	
		//componentes fila 1
		valueBusquedaText = new Text(compositeBusquedaVehiculo, SWT.RIGHT | SWT.BORDER);
		valueBusquedaText.addKeyListener(new KeyListener(){
			String selectedItem;
			public void keyPressed(KeyEvent e) {
				if(e.keyCode==13){
					findVehiculoByPlate(seleccionTipoBusquedaCombo.getSelectionIndex());
				};
			}
			public void keyReleased(KeyEvent e) {
				return;
			}
		});
		FormData formData = new FormData();
		formData.left = new FormAttachment(44);
		valueBusquedaText.setLayoutData(formData);
		
		seleccionTipoBusquedaCombo = new Combo(compositeBusquedaVehiculo, SWT.READ_ONLY);
		formData = new FormData();
		formData.left= new FormAttachment(valueBusquedaText,5);
		seleccionTipoBusquedaCombo.setLayoutData(formData);
		seleccionTipoBusquedaCombo.add("Placa");
		seleccionTipoBusquedaCombo.add("Codigo");
		seleccionTipoBusquedaCombo.select(0);
				
		Button buscarVehiculoButton = new Button(compositeBusquedaVehiculo,SWT.PUSH|SWT.CENTER);
		buscarVehiculoButton.setText(entradaSalidaUtil.getProperty("window.buscarVehiculoButtonLabel"));
		formData=new FormData();
		formData.right=new FormAttachment(100);
		formData.width=100;
		formData.left= new FormAttachment(seleccionTipoBusquedaCombo,5);
		buscarVehiculoButton.setLayoutData(formData);
		buscarVehiculoButton.setImage(entradaSalidaUtil.getImagesMap().get("searchImage"));
		buscarVehiculoButton.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent event) {
				
				findVehiculoByPlate(seleccionTipoBusquedaCombo.getSelectionIndex());
				
			}
		});
		
		abrirCatalogoVehiculoButton.setText(entradaSalidaUtil.getProperty("window.abrirCatalogoVehiculoButtonLabel"));
		abrirCatalogoVehiculoButton.setImage(entradaSalidaUtil.getImagesMap().get("vehiculoImage"));
		abrirCatalogoVehiculoButton.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent event) {
				//dialog de busqueda de vehiculo
				//inicializamos dialog para la busqueda del codigo de vehiculo
				seleccionVehiculoDialog = (SeleccionVehiculoDialog)ApplicationContextUtil.getBean("seleccionVehiculoDialog");
				seleccionVehiculoDialog.populateList();
				vehiculo = seleccionVehiculoDialog.open();
				setVehiculoFormData(vehiculo);
			}
		});
		
		//------Grupo Seleccion Chofer -----------------------------------------------------------------------------------------------------
		
		Group groupSeleccionChofer = new Group(group,SWT.SHADOW_ETCHED_IN);
		groupSeleccionChofer.setText(entradaSalidaUtil.getProperty("window.seleccionChoferLabel"));
		
		gridData.grabExcessHorizontalSpace= true;
		gridData.horizontalAlignment=SWT.FILL;
		groupSeleccionChofer.setLayoutData(new GridData(SWT.FILL,SWT.NULL,true,true));
		GridLayout gridSeleccionChoferLayoutGroup = new GridLayout(3,true);
		gridSeleccionChoferLayoutGroup.marginHeight=10;
		gridSeleccionChoferLayoutGroup.marginRight=10;
		gridSeleccionChoferLayoutGroup.marginLeft=10;
		gridSeleccionChoferLayoutGroup.verticalSpacing=10;
		groupSeleccionChofer.setLayout(gridSeleccionChoferLayoutGroup);
		
		//fila1
		Composite compositeBusquedaChofer = new Composite(groupSeleccionChofer,SWT.SHADOW_NONE);
		gridData = new GridData(SWT.FILL,SWT.NULL,true,false);
		gridData.horizontalSpan=3;
		compositeBusquedaChofer.setLayoutData(gridData);
		
		formLayout = new FormLayout();
		formLayout.marginLeft=0;
		formLayout.marginBottom=0;
		formLayout.marginTop=0;
		formLayout.marginRight=0;
		compositeBusquedaChofer.setLayout(formLayout);
		
		//fila2
		abrirCatalogoChoferButton = new Button(groupSeleccionChofer,SWT.PUSH|SWT.RIGHT);
		abrirCatalogoChoferButton.setLayoutData(new GridData(GridData.END,GridData.VERTICAL_ALIGN_CENTER,true,true,3,0));
		
		//fila7
		Label codigoChoferLabel = new Label(groupSeleccionChofer,SWT.RIGHT);
		gridData = new GridData(GridData.FILL, GridData.VERTICAL_ALIGN_CENTER,true,false);
		codigoChoferLabel.setLayoutData(gridData);
		codigoChoferLabel.setText(entradaSalidaUtil.getProperty("window.codigoChoferLabel"));
	
		codigoChoferValueLabel = new Label(groupSeleccionChofer, SWT.LEFT);
		codigoChoferValueLabel.setText("-- --");
		gridData = new GridData(GridData.FILL, GridData.CENTER,true,false);
		gridData.grabExcessHorizontalSpace= true;
		gridData.horizontalSpan=2;
		codigoChoferValueLabel.setLayoutData(gridData);
		
		//fila
		Label cedulaChoferLabel = new Label(groupSeleccionChofer,SWT.RIGHT);
		gridData = new GridData(GridData.FILL, GridData.VERTICAL_ALIGN_CENTER,true,false);
		cedulaChoferLabel.setLayoutData(gridData);
		cedulaChoferLabel.setText(entradaSalidaUtil.getProperty("window.cedulaChoferLabel"));
	
		cedulaChoferValueLabel = new Label(groupSeleccionChofer, SWT.LEFT|SWT.FILL);
		cedulaChoferValueLabel.setText("-- --");
		gridData = new GridData(GridData.FILL, GridData.CENTER,true,false);
		gridData.grabExcessHorizontalSpace= true;
		gridData.horizontalSpan=2;
		cedulaChoferValueLabel .setLayoutData(gridData);
		
		//fila
		Label nombreChoferLabel = new Label(groupSeleccionChofer,SWT.RIGHT);
		gridData = new GridData(GridData.FILL, GridData.VERTICAL_ALIGN_CENTER,true,false);
		nombreChoferLabel.setLayoutData(gridData);
		nombreChoferLabel.setText(entradaSalidaUtil.getProperty("window.nombreChoferLabel"));
	
		nombreChoferValueLabel = new Label(groupSeleccionChofer, SWT.LEFT|SWT.FILL);
		nombreChoferValueLabel.setText("-- --");
		gridData = new GridData(GridData.FILL, GridData.CENTER,true,false);
		gridData.grabExcessHorizontalSpace= true;
		gridData.horizontalSpan=2;
		nombreChoferValueLabel.setLayoutData(gridData);

		//fila7
		Composite compositeStatusSeleccionChofer = new Composite(groupSeleccionChofer,SWT.BOTTOM);
		gridData = new GridData(SWT.FILL,GridData.VERTICAL_ALIGN_END,true,false);
		gridData.horizontalSpan=3;
		compositeStatusSeleccionChofer.setLayoutData(gridData);
		compositeStatusSeleccionChofer.setLayout(rowLayout);
		
		Label statusSeleccionChofer = new Label(compositeStatusSeleccionChofer,SWT.LEFT);
		statusSeleccionChofer.setText(entradaSalidaUtil.getProperty("window.choferSeleccionadoLabel"));
		statusSeleccionChofer.setFont(fuente);
		
		imageDeleteChofer = new Label(compositeStatusSeleccionChofer,SWT.LEFT);
		imageDeleteChofer.setImage(entradaSalidaUtil.getImagesMap().get("deleteImage"));
	
		//componentes fila 1
		valueBusquedaChoferText = new Text(compositeBusquedaChofer, SWT.RIGHT | SWT.BORDER);
		formData = new FormData();
		formData.left = new FormAttachment(44);
		valueBusquedaChoferText.setLayoutData(formData);
		valueBusquedaChoferText.addKeyListener(new KeyListener(){

			public void keyPressed(KeyEvent e) {
				if(e.keyCode==13){
					findChoferByCI(seleccionTipoBusquedaChoferCombo.getSelectionIndex());
				};
			}
			public void keyReleased(KeyEvent e) {
				return;
			}
		});
		
		seleccionTipoBusquedaChoferCombo = new Combo(compositeBusquedaChofer, SWT.READ_ONLY);
		formData = new FormData();
		formData.left= new FormAttachment(valueBusquedaChoferText,5);
		seleccionTipoBusquedaChoferCombo.setLayoutData(formData);
		seleccionTipoBusquedaChoferCombo.add("Cedula");
		seleccionTipoBusquedaChoferCombo.add("Codigo");
		seleccionTipoBusquedaChoferCombo.select(0);
				
		buscarChoferButton = new Button(compositeBusquedaChofer,SWT.PUSH|SWT.CENTER);
		buscarChoferButton.setText(entradaSalidaUtil.getProperty("window.buscarVehiculoButtonLabel"));
		formData=new FormData();
		formData.right=new FormAttachment(100);
		formData.width=100;
		formData.left= new FormAttachment(seleccionTipoBusquedaChoferCombo,5);
		buscarChoferButton.setLayoutData(formData);
		buscarChoferButton.setImage(entradaSalidaUtil.getImagesMap().get("searchImage"));
		buscarChoferButton.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent event) {
				findChoferByCI(seleccionTipoBusquedaChoferCombo.getSelectionIndex());
			}
		});
		
		abrirCatalogoChoferButton.setText(entradaSalidaUtil.getProperty("window.buscarCodigoChoferButtonLabel"));
		abrirCatalogoChoferButton.setImage(entradaSalidaUtil.getImagesMap().get("userImage"));
		abrirCatalogoChoferButton.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent event) {
				
				seleccionChoferDialog = (SeleccionChoferDialog)ApplicationContextUtil.getBean("seleccionChoferDialog");
				seleccionChoferDialog.populateList();
				chofer  = seleccionChoferDialog.open();
				setChoferFormData(chofer);
				
			}
		});
		
		
		//------------------------------------------------------------------------------------------------
		groupSeleccionPeso = new Group(group,SWT.SHADOW_ETCHED_IN);
		groupSeleccionPeso.setText(entradaSalidaUtil.getProperty("window.seleccionPesoLabel"));
		gridData.grabExcessHorizontalSpace= true;
		gridData.horizontalAlignment=SWT.FILL;
		groupSeleccionPeso.setLayoutData(new GridData(SWT.FILL,SWT.NULL,true,true));
		
		GridLayout gridSeleccionPesoLayoutGroup = new GridLayout(3,true);
		gridSeleccionPesoLayoutGroup.marginHeight=10;
		gridSeleccionPesoLayoutGroup.marginRight=10;
		gridSeleccionPesoLayoutGroup.marginLeft=10;
		gridSeleccionPesoLayoutGroup.verticalSpacing=10;
		groupSeleccionPeso.setLayout(gridSeleccionPesoLayoutGroup);
		
		//fila8
		Label pesoLabel = new Label(groupSeleccionPeso,SWT.RIGHT);
		gridData = new GridData(GridData.FILL, GridData.END,true,false);
		pesoLabel.setLayoutData(gridData);
		pesoLabel.setText(entradaSalidaUtil.getProperty("window.pesoLabel"));
		Label pesoValueLabel = new Label(groupSeleccionPeso, SWT.LEFT|SWT.FILL);
		pesoValueLabel.setText("-- --");
		gridData = new GridData(GridData.FILL, GridData.CENTER,true,false);
		gridData.grabExcessHorizontalSpace= true;
		gridData.horizontalSpan=2;
		pesoValueLabel.setLayoutData(gridData);

		//fila9
		Label pesoTaraLabel = new Label(groupSeleccionPeso,SWT.RIGHT);
		gridData = new GridData(GridData.FILL, GridData.END,true,false);
		pesoTaraLabel.setLayoutData(gridData);
		pesoTaraLabel.setText(entradaSalidaUtil.getProperty("window.pesoTaraLabel"));
		pesoTaraValueText = new Text(groupSeleccionPeso, SWT.LEFT|SWT.FILL);
		//pesoTaraValueLabel.setText("-- --");
		gridData = new GridData(GridData.FILL, GridData.CENTER,true,false);
		gridData.grabExcessHorizontalSpace= true;
		gridData.horizontalSpan=2;
		pesoTaraValueText.setLayoutData(gridData);
		
		//-----------------------------------------------------------------------------------------------------
		
		GridLayout gridLayoutShell = new GridLayout(1,true);
		gridLayoutShell.verticalSpacing=20;
		gridLayoutShell.marginHeight=20;
		gridLayoutShell.marginLeft=10;
		gridLayoutShell.marginRight=10;
		shell.setLayout(gridLayoutShell);

		//boton aceptar
		aceptarButton.setText("Guardar");
		aceptarButton.setImage(entradaSalidaUtil.getImagesMap().get("acceptImage"));
		aceptarButton.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent event) {
				if(validateParadaForm()){
					String pesoString=""; 
					if(peso)pesoString=pesoTaraValueText.getText(); 
					else pesoString = "0";
					
					boolean response = entradaSalidaBDL.persistParada(vehiculo,chofer,pesoString,entrada);
					
					if (response){
						//se agrego correctamente
						alert("window.aceptarMessage");
						cleanVehiculoFormData();
						cleanChoferFormData();
						
					}else{
						alert("window.aceptarMessageError");
					}
				}
			}
		});
		gridData = new GridData();
		gridData.horizontalAlignment = SWT.CENTER;
		gridData.grabExcessHorizontalSpace=false;
		aceptarButton.setLayoutData(gridData);

		
	}
	
	public boolean validateParadaForm(){
		if(codigoChoferValueLabel.getText().equals("-- --")){ 
			alert("window.debeSeleccionarChoferMessage");
			return false;
		}
		
		if(codigoVehiculoValueLabel.getText().equals("-- --")){ 
			alert("window.debeSeleccionarVehiculoMessage");
			return false;
		}
		
		if(peso && pesoTaraValueText.getText().equals("") ){ 
			alert("window.debeIngresarPesoMessage");
			return false;
		}else{
			if(peso && !entradaSalidaUtil.isIntNumber(pesoTaraValueText.getText())){
				alert("window.pesoNumericoMessage");
				return false;
			}
		}
		
		
		return true;
	}
	
	public void alert(String propertyString){
		dialog.setMessage (entradaSalidaUtil.getProperty(propertyString));
		dialog.setText(entradaSalidaUtil.getProperty("seleccionChoferDialog.informacionLabel"));
		dialog.open();
	}
	
	public boolean 	findVehiculoByPlate(int selection){
		Vehiculo vehiculo;
		switch(selection){
			case 0: //placa
				
				vehiculo = entradaSalidaBDL.getVehiculoByPlaca(valueBusquedaText.getText());
				
				if(vehiculo!=null){
					setVehiculoFormData(vehiculo);
				}else{
					MessageBox dialog = new MessageBox (shell, SWT.OK|SWT.ICON_WARNING);
					dialog.setMessage ("No se encontro un vehiculo con esa placa!");
					dialog.setText(entradaSalidaUtil.getProperty("seleccionChoferDialog.informacionLabel"));
					dialog.open();
					
				}
				
			break;
			
			case 1: //codigo vehiculo
				vehiculo = entradaSalidaBDL.getVehiculoByCodigo(valueBusquedaText.getText());
				
				if(vehiculo!=null){
					setVehiculoFormData(vehiculo);
				}else{
					MessageBox dialog = new MessageBox (shell, SWT.OK|SWT.ICON_WARNING);
					dialog.setMessage ("No se encontro un vehiculo con ese codigo!");
					dialog.setText(entradaSalidaUtil.getProperty("seleccionChoferDialog.informacionLabel"));
					dialog.open();
				}
		
			break;
				
		}
		
		return true;
	}
	
	public boolean findChoferByCI(int selection){
		Chofer chofer;
		
		switch (selection ){
			case 0://cedula
				
				chofer = entradaSalidaBDL.getChoferByCI(valueBusquedaChoferText.getText());
				
				if(chofer!=null){
					setChoferFormData(chofer);
				}else{
					cleanChoferFormData();
					MessageBox dialog = new MessageBox (shell, SWT.OK|SWT.ICON_WARNING);
					dialog.setMessage ("No se encontro un chofer con esa CI!");
					dialog.setText(entradaSalidaUtil.getProperty("seleccionChoferDialog.informacionLabel"));
					dialog.open();
				}
			
				break;
			case 1://codigo
		
				chofer = entradaSalidaBDL.getChoferByCodigo(valueBusquedaChoferText.getText());
				
				if(chofer!=null){
					setChoferFormData(chofer);
				}else{
					cleanChoferFormData();
					MessageBox dialog = new MessageBox (shell, SWT.OK|SWT.ICON_WARNING);
					dialog.setMessage ("No se encontro un chofer con ese Codigo!");
					dialog.setText(entradaSalidaUtil.getProperty("seleccionChoferDialog.informacionLabel"));
					dialog.open();
				}
					
			break; 
		
		}
		return true;
	}
	
	
public void setVehiculoFormData(Vehiculo vehiculo){
	this.vehiculo = vehiculo;
	codigoVehiculoValueLabel.setText(vehiculo.getCodigo());
	placaVehiculoValueLabel.setText(vehiculo.getPlaca());
	descripcionValueLabel.setText(vehiculo.getDescripcion());
	//clasificacionValueLabel.setText(vehiculo());
	capacidadValueLabel.setText(vehiculo.getCapacidad().toString());
	organismoValueLabel.setText(vehiculo.getOrganizacionId().getDenominacion());
	imageDeleteVehiculo.setImage(entradaSalidaUtil.getImagesMap().get("checkImage"));
	
}	

public void cleanVehiculoFormData(){
	
	codigoVehiculoValueLabel.setText("-- --");
	placaVehiculoValueLabel.setText("-- --");
	descripcionValueLabel.setText("-- --");
	//clasificacionValueLabel.setText("-- --");
	capacidadValueLabel.setText("-- --");
	organismoValueLabel.setText("-- --");
	imageDeleteVehiculo.setImage(entradaSalidaUtil.getImagesMap().get("deleteImage"));
	
} 

public void setChoferFormData(Chofer chofer){
	this.chofer = chofer;
	codigoChoferValueLabel.setText(chofer.getChoferId().toString());	
	cedulaChoferValueLabel.setText(chofer.getCedula().toString());
	nombreChoferValueLabel.setText(chofer.getApellidos()+", "+ chofer.getNombres());
	imageDeleteChofer.setImage( entradaSalidaUtil.getImagesMap().get("checkImage"));
	
}

public void cleanChoferFormData(){
	codigoChoferValueLabel.setText("-- --");	
	cedulaChoferValueLabel.setText("-- --");
	nombreChoferValueLabel.setText("-- --");
	imageDeleteChofer.setImage( entradaSalidaUtil.getImagesMap().get("deleteImage"));
}

public void loadModifyWindowSettings(){
	
	//chuck berry - hoochie coochie man
	
	//Especificacion si es de entrada o salida la aplicacion
	entrada = new Boolean(entradaSalidaUtil.getConfigProperty("basura.config.entrada")).booleanValue();
	peso = new Boolean(entradaSalidaUtil.getConfigProperty("basura.config.peso")).booleanValue();
	
	if(entrada){
		tituloLabel.setText(entradaSalidaUtil.getProperty("window.groupEntradaLabel"));
		group.setText(entradaSalidaUtil.getProperty("window.tituloEntradaLabel"));
	}else{
		tituloLabel.setText(entradaSalidaUtil.getProperty("window.groupSalidaLabel"));
		group.setText(entradaSalidaUtil.getProperty("window.tituloSalidaLabel"));
	}
	
	//Especificacion si se requiere el peso o no
	if(!peso){
		groupSeleccionPeso.dispose();
		shell.setSize(550, 770);
		
	}else{
		shell.setSize(550, 840);
	}
	
}

 public boolean	selectChoferEntry(){
	 
	// seleccionChoferDialog.getTableSelection();
	 return true; 
	 
 }
	
 	/**
	 * @param entradaSalidaBDL the entradaSalidaBDL to set
	 */
	public void setEntradaSalidaBDL(EntradaSalidaBDL entradaSalidaBDL) {
		this.entradaSalidaBDL = entradaSalidaBDL;
	}
	
}		
